/*************************************************************************
 * File Name:    Sum_Root_to_Leaf_Numbers.cc
 * Author:       zero91
 * Mail:         jianzhang9102@gmail.com
 * Created Time: Tue 17 Sep 2013 11:50:47 PM CST
 * 
 * Description:  
 *
 * ----------------------------------------------------------------------
 |
 | Sum Root to Leaf Numbers
 |
 | Given a binary tree containing digits from 0-9 only, each root-to-leaf
 | path could represent a number.
 |
 | An example is the root-to-leaf path 1->2->3 which represents the number 123.
 |
 | Find the total sum of all root-to-leaf numbers.
 |
 | For example,
 |
 |     1
 |    / \
 |   2   3
 |  The root-to-leaf path 1->2 represents the number 12.
 |  The root-to-leaf path 1->3 represents the number 13.
 |
 |  Return the sum = 12 + 13 = 25.
 |
 | ----------------------------------------------------------------------
 |
 ************************************************************************/

#include <iostream>

using namespace std;

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
private:
    int sum;

    void traverse(TreeNode *root, int num)
    {
        if (root->left == NULL && root->right == NULL) {
            sum += num * 10 + root->val;
            return;
        }

        if (root->left != NULL) {
            traverse(root->left, num * 10 + root->val);
        }
            
        if (root->right != NULL) {
            traverse(root->right, num * 10 + root->val);
        }
    }

public:
    int sumNumbers(TreeNode *root)
    {
        if (root == NULL) return 0;

        sum = 0;
        traverse(root, 0);
        return sum;
    }
};

int
main(int argc, char *argv[])
{

    return 0;
}
